package DesignPatern.A15_组合模式.QSB.A02_优化基础模式;

import java.util.ArrayList;

/**
 * @Author: Du
 * @Date: 2021/8/2 10:18
 */
public class A01_Main
{
    public static void main(String[] args)
    {
        Branch ceo = compositeCorpTree();
        System.out.println(ceo.getInfo());
        System.out.println(getTreeInfo(ceo));
    }
    
    public static Branch compositeCorpTree()
    {
        Branch root = new Branch("zs", "总经理", 11111);
        Branch devDop = new Branch("ls", "市场总经理", 11111);
        Branch salDop = new Branch("ww", "研发总经理", 11111);
        
        Leaf a = new Leaf("a", "市场人员1", 2001);
        Leaf b = new Leaf("b", "市场人员2", 2002);
        Leaf c = new Leaf("c", "市场人员3", 2003);
        Leaf d = new Leaf("a", "开发人员1", 2004);
        Leaf e = new Leaf("b", "开发人员2", 2005);
        Leaf f = new Leaf("c", "开发人员3", 2006);
        
        root.addSubList(devDop);
        root.addSubList(salDop);
        
        salDop.addSubList(a);
        salDop.addSubList(b);
        salDop.addSubList(c);
        
        devDop.addSubList(d);
        devDop.addSubList(e);
        devDop.addSubList(f);
        
        return root;
    }
    
    public static String getTreeInfo(Branch root)
    {
        ArrayList<ICrop> subList = root.getSubList();
        String Info = "";
        
        for (ICrop iCrop : subList)
        {
            if (iCrop instanceof Leaf)
            {
                Info += iCrop.getInfo() + "\n";
            } else
            {
                
                Info += iCrop.getInfo() + "\n" + getTreeInfo((Branch) iCrop) + "\n";
            }
        }
        return Info;
    }
}
